package cn.xo68.ide.plugin.common.job;

import cn.xo68.ide.plugin.common.message.TaskListener;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ToolSuiteTask extends Task.Backgroundable {

    protected final ToolSuiteJob job;

    public ToolSuiteTask(@Nullable Project project,ToolSuiteJob job) {
        super(project, job.getTitle(), true);

        this.job = job;
    }

    @Override
    public boolean shouldStartInBackground() {
        return getJob().isStartInBackground();
    }

    @Override
    public boolean isConditionalModal() {
        return getJob().isModal();
    }

    public ToolSuiteJob getJob() {
        return job;
    }

    @Override
    public void run(@NotNull ProgressIndicator indicator) {
        try {
            this.job.execute(indicator);
            getJob().getCallback().success();
        } catch (Throwable e) {
            //handleError(e, indicator);
            getJob().getCallback().fail(e);
        } finally {
            this.getProject().getMessageBus().syncPublisher(TaskListener.TASK_TOPIC).ended(job);
        }

    }
}
